package org.jparrow.framework.proxy;

import java.lang.reflect.Method;
import java.util.List;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * 代理对象管理器
 * 
 * @author supda
 * @Date 2017-3-29 上午9:53:24
 *
 */
public class ProxyManager {

	/**
	 * 创建代理对象
	 */
	@SuppressWarnings("unchecked")
	public static <T> T createProxy(final Class<?> targetClass,
			final List<Proxy> proxyList) {
		return (T) Enhancer.create(targetClass, new MethodInterceptor() {
			
			/**
			 * 执行代理方法
			 */
			@Override
			public Object intercept(Object targetObject, 
					Method targetMethod,
					Object[] methodParams,
					MethodProxy methodProxy) throws Throwable {
				
				return new ProxyChain(targetClass, targetObject, targetMethod
						,methodProxy,methodParams,proxyList).doProxyChain();
			}
		});
	}
	
}
